package com.xiaoq.positionmarket.endpoint;

import com.xiaoq.BaseEndpoint2B;
import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.positionmarket.entity.UserTrackEntity;
import com.xiaoq.positionmarket.service.IUserTrackService;
import com.xiaoq.positionmarket.vo.InsertUserTrackVo;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chenjh on 018/4/2.
 */
@RestController
@RequestMapping("/api/v1/usertrack")
@Api(tags = "用户位置数据")
public class UserTrackEndPoint2C extends BaseEndpoint2B{

    @Autowired
    private IUserTrackService userTrackService;

    @Autowired
    private IdGenerator idGenerator;

    private static final class UserLocationTrackRateResponse extends GenericDataResponse<UserLocationTrackRateVo> {}


    @ApiOperation(value = "新增用户位置数据", notes = "新增用户位置数据")
    @ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "新增用户位置数据") })
    @PostMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
    public ResponseEntity<?> insert(@RequestBody InsertUserTrackVo userTrackVo, HttpServletRequest request){

        String userId = (String) request.getAttribute("userId");

        if(userTrackVo.getLatitude() == null || userTrackVo.getLongitude() == null
                || StringUtils.isEmpty(userTrackVo.getRegId()) || StringUtils.isEmpty(userTrackVo.getOsType())){
            return GenericResponse.ok();
        }

        UserTrackEntity userTrackEntity = new UserTrackEntity();
        BeanUtils.copyProperties(userTrackVo, userTrackEntity);
        userTrackEntity.setId(idGenerator.nextId());
        userTrackEntity.setUserId(userId);
        userTrackService.save(userTrackEntity);
        return GenericResponse.ok();
    }

    @ApiOperation(value = "获取用户定位频率", notes = "获取用户定位频率")
    @ApiResponses({ @ApiResponse(code = 200, response = UserLocationTrackRateResponse.class, message = "获取用户定位频率") })
    @GetMapping(value = "/rate", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
    public ResponseEntity<?> rate(){
        UserLocationTrackRateVo userLocationTrackRateVo = new UserLocationTrackRateVo();
        return UserLocationTrackRateResponse.okWithData(userLocationTrackRateVo);
    }


    public class UserLocationTrackRateVo {

        @ApiModelProperty(name = "rate", value = "分钟", position=-1)
        private int rate = 50;

        public int getRate() {
            return rate;
        }

        public void setRate(int rate) {
            this.rate = rate;
        }
    }


}





